/** *
 *  {initData} 监听事件拿到的data.data
 *  {inputJson} 对应的转换数据
 *
 * * */
function saasTosj(resData, inputJson, gdzczjJson) {
  let data = resData.saasData.data.hsqjData || [];

  let insType = resData.saasData.insType;

  const inputData = Object.entries(inputJson).reduce((result, item) => {
    const saasData = data;
    const smallData = item[1].small;
    const generData = item[1].gener;
    const resultData = saasData.find(dataItem => {
      const { subjectCode } = dataItem;
      return (
        subjectCode === smallData.subjectCode || subjectCode === generData.subjectCode
      );
    }) || {};

    if (smallData.formula) {
      const amt = resultData.amt || 0;
      if (insType === '2007') {
        result[item[0]] = generData.formula.call(
          inputJson,
          amt,
          result,
          saasData
        );
      } else {
        result[item[0]] = smallData.formula.call(
          inputJson,
          amt,
          result,
          saasData
        );
      }
    } else {
      result[item[0]] = resultData.amt || '0.00';
    }
    return result;
  }, {});

  let gdzczj = getGDZCDATA(resData, gdzczjJson);

  return { ...inputData, ...gdzczj };
}


function getGDZCDATA(resData, gdzczjJson) {
  let assetAmt = resData.saasData.data.assetAmt || [];
  let obj = {
    // 房屋建筑物
    // 机器设备
    // 办公设备
    // 运输设备
    // 电子设备
  };
  let keys = Object.keys(gdzczjJson);
  assetAmt.forEach(item => {
    let assetsName = item.assetsName;
    if (keys.includes(assetsName)) {
      let vkobj = gdzczjJson[assetsName];
      for (let k in vkobj) {
        let v = vkobj[k];
        obj[k] = item[v];
      }
    }
  });

  return obj;
}

// 判断是否需要转化为json字符串
function isChangeJson(data) {
  let urlsJson = [
    'https://www.etax-gd.gov.cn/',
    "https://etax.guangdong.chinatax.gov.cn/",
    "https://etax.beijing.chinatax.gov.cn/",

  ];
  const href = window.location.href;
  if (urlsJson.find(item => href.includes(item))) {
    return JSON.stringify(data)
  }
  return data;
}

/**
 * {object} info 对象{success: 是否填报成功, tax: 税种, info: 文案}
 *
 * * */
function fillInputDone(info) {
  setTimeout(() => {
    window.postMessage(
      {
        event: 'fillInputDone',
        data: isChangeJson({
          message: info.message
        })
      },
      '*'
    );
  }, 3000);
}

/** *
 * {string} message 提示信息 必传
 * {object} option  操作 不必传，暂时没用
 *
 */
async function notifications(message, option = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      window.postMessage(
        {
          event: 'notifications',
          data: isChangeJson({
            message: message,
            option
          })
        },
        '*'
      );
      resolve();
    }, 500);
  });
}



// 等待时间 timeout 时间
async function waitTimeOut(timeout = 1000) {
  return new Promise(function (resolve, reject) {
    setTimeout(() => {
      resolve();
    }, timeout);
  });
}

// 是否需要填报前年的数据
function isFillYearBeforeLast({ companyInfo }) {
  var date = new Date;
  var current = date.getFullYear()
  let { startDate = current } = companyInfo;
  return (current - parseInt(startDate.split('-')[0])) >= 2;
}
export {
  saasTosj,
  fillInputDone,
  waitTimeOut,
  notifications,
  isFillYearBeforeLast,
};
